<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-7-24
 * Time: 9:31
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\Validate;

class BulletinController extends AdminBaseController
{
    public function index()
    {
        $param    = $this->request->param();
        $b_title = isset($param['b_title']) ? $param['b_title'] : '';
        $where['b_type'] = 10;
        if($b_title){ $where['b_title'] = ['like', "%$b_title%"];}else{ unset($param['b_title']);}
        $bulletin = DB::name('bulletin')
            ->where($where)
            ->order(['id' => 'DESC'])
            ->paginate(20);
        $bulletin->appends($param);
        $page = $bulletin->render();
        $this->assign("b_title", $b_title);
        $this->assign("bulletin", $bulletin);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'b_title' => 'require|chsAlphaNum|min:4|max:32|unique:bulletin,b_title',
                'b_lasttime' => 'require|dateFormat:Y-m-d H:i|after:+3 day|before:+3 month|unique:bulletin,b_lasttime',
                'b_description' => 'require|min:16|max:128',
            ]);
            $validate->message([
                'b_title.require' => '公告名称不可为空',
                'b_title.chsAlpha' => '公告名称只能是只能是汉字、字母和数字',
                'b_title.min' => '公告名称最小长度为4个汉字或字母',
                'b_title.max' => '公告名称最大长度为32个汉字或字母',
                'b_title.unique' => '公告名称已存在',
                'b_lasttime.require' => '有效期不能为空',
                'b_lasttime.dateFormat' => '有效期格式不正确',
                'b_lasttime.after' => '有效期时间过短',
                'b_lasttime.before' => '有效期时间过长',
                'b_description.require' => '公告内容不可为空',
                'b_description.min' => '公告内容最小长度为16个字符',
                'b_description.max' => '公告内容最大长度为128个字符',
                'b_description.unique' => '公告内容已存在',
            ]);
            $data = $this->request->post();
            if ($validate->check($data)) {
                $data['b_udid'] = 0;
                $data['b_type'] = 10;
                $data['b_userid'] = cmf_get_current_admin_id();
                Db::name('bulletin')->insert($data);
                $this->success('添加成功','bulletin/index');
            }else{
                $this->error($validate->getError());
            }
        }else{
            $this->error('请求错误');
        }
    }

    public function edit()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $bulletin = DB::name('bulletin')->where(["id" => $id])->find();
        $this->assign("bulletin", $bulletin);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'b_title' => 'require|chsAlphaNum|min:4|max:32',
                'b_lasttime' => 'require|dateFormat:Y-m-d H:i|after:+3 day|before:+3 month',
                'b_description' => 'require|min:16|max:128',
            ]);
            $validate->message([
                'b_title.require' => '公告名称不可为空',
                'b_title.chsAlpha' => '公告名称只能是只能是汉字、字母和数字',
                'b_title.min' => '公告名称最小长度为4个汉字或字母',
                'b_title.max' => '公告名称最大长度为32个汉字或字母',
                'b_lasttime.require' => '有效期不能为空',
                'b_lasttime.dateFormat' => '有效期格式不正确',
                'b_lasttime.after' => '有效期时间过短',
                'b_lasttime.before' => '有效期时间过长',
                'b_description.require' => '公告内容不可为空',
                'b_description.min' => '公告内容最小长度为16个字符',
                'b_description.max' => '公告内容最大长度为128个字符',
            ]);
            $data = $this->request->post();
            if ($validate->check($data)) {
                $data['b_udid'] = 0;
                $data['b_type'] = 10;
                $data['b_userid'] = cmf_get_current_admin_id();
                Db::name('bulletin')->update($data);
                $this->success('更新成功','bulletin/index');
            }else{
                $this->error($validate->getError());
            }
        }else{
            $this->error('请求错误');
        }
    }
}